Passed
Push — master ( 91e4ab...1def37 )
by Plamen
01:28
created

table_methods.Init.SetColumnsHoverEffect   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 2
b 0
f 0
nc 3
nop 2
dl 0
loc 11
rs 10
1
var table_methods = {
2
    helper: window.table_helper,
3
    BuildRequest: {
4
        Sort: function(rq){
5
            function sortBySpan(span, i){
6
                var order = span.innerHTML;
7
                if(order.length === 1){
8
                    rq.colNo = i;
9
                    rq.colOrd = order === rq.strDesc ? "desc" : "asc";
10
                }
11
                return rq.colNo === i;
12
            }
13
            var thTags = document.getElementById(rq.tableId)
14
                    .getElementsByTagName("thead")[0]
15
                    .getElementsByTagName("th");
16
            var length = thTags.length;
17
            for(var i = 0; i < length; i++){
18
                var link = thTags[i].getElementsByTagName("a")[0];
19
                if(link){
20
                    var span = link.getElementsByTagName("span")[0];
21
                    if(span && sortBySpan(span, i)){
22
                        break;
23
                    }
24
                }
25
            }
26
        },
27
        Filter: function(rq){
28
            function getFilterFieldsByTableID(tableID){
29
                var fields = {filterBy: null, filter: null};
30
                var filterDiv = getFilterDivByTableIDOrNull(tableID);
31
                if(filterDiv !== null){
32
                    setFilterBy(fields, filterDiv);
33
                    setFilterValue(fields, filterDiv);
34
                }
35
                return fields;
36
            }
37
            function getFilterDivByTableIDOrNull(tableID){
38
                if(document.getElementById(tableID).parentNode
39
                        .getElementsByTagName("div").length > 0
40
                        ){
41
                    var containerDivs = document.getElementById(tableID)
42
                            .parentNode.getElementsByTagName("div");
43
                    for(var i = 0; i < containerDivs.length; i++){
44
                        if(containerDivs[i].getAttribute("class") === "filter"){
45
                            return containerDivs[i];
46
                        }
47
                    }
48
49
                }
50
                return null;
51
            }
52
            function setFilterBy(fields, filterDiv){
53
                var select = filterDiv.getElementsByTagName("select")[0];
54
                if(select &&
55
                        select.options[select.selectedIndex].value !== "all"
56
                        ){
57
                    fields.filterBy = select.options[select.selectedIndex].value;
58
                }
59
            }
60
            function setFilterValue(fields, filterDiv){
61
                var textObj = filterDiv.getElementsByTagName("input")[0];
62
                if(textObj && textObj.value && textObj.value.length !== 0){
63
                    fields.filter = encodeURIComponent(textObj.value.trim());
64
                }
65
            }
66
67
            var r = getFilterFieldsByTableID(rq.tableId);
68
            if(r.filter !== null){
69
                rq.filter = r.filter;
70
            }
71
            if(r.filterBy !== null){
72
                rq.filterBy = r.filterBy;
73
            }
74
        },
75
        Run: function(rq){
76
            this.Sort(rq);
77
            this.Filter(rq);
78
        }
79
    },
80
    Draw: {
81
        Section: function(tableContainer, dt, tSection){
82
            var section = tSection === "tfoot" ? "tfoot" : "tbody";
83
            var tSec = document.getElementById(tableContainer)
84
                    .getElementsByTagName(section)[0];
85
            Clear(tSec, this.helper.IePrior(9));
86
            for(var i = 0; i < dt.length; i++){
87
                var row = dt[i];
88
                var tRow = document.createElement("tr");
89
                Row(row, tRow);
90
                tSec.appendChild(tRow);
91
                if(section === "tfoot"){
92
                    this.helper.ProcessPaginationLinks(tSec);
93
                }
94
            }
95
            function Clear(tSection, iePrior9){
96
                if(iePrior9){
97
                    if(tSection.firstChild){
98
                        while(tSection.firstChild){
99
                            tSection.removeChild(tSection.firstChild);
100
                        }
101
                    }
102
                }else{
103
                    tSection.innerHTML = "";
104
                }
105
            }
106
            function Row(row, tRow){
107
                for(var cell in row){
1 ignored issue
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
108
                    var tCell = document.createElement("td");
109
                    if(typeof row[cell] === "string" ||
110
                            typeof row[cell] === "number"
111
                            ){
112
                        tCell.innerHTML = row[cell];
113
                    }else if(typeof row[cell] === "object"){
114
                        RowCellFromObject(row, cell, tCell);
115
                    }
116
                    tRow.appendChild(tCell);
117
                }
118
            }
119
            function RowCellFromObject(row, cell, tCell){
120
                for(var attr in row[cell]){
1 ignored issue
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
121
                    if(typeof row[cell][attr] === "string"){
122
                        tCell.innerHTML = row[cell][attr];
123
                    }else if(typeof row[cell][attr] === "object"){
124
                        for(var v in row[cell][attr]){
1 ignored issue
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
125
                            tCell.setAttribute(v, row[cell][attr][v]);
126
                        }
127
                    }
128
                }
129
            }
130
        },
131
        Run: function(tableContainer, d, instance){
132
133
            this.Section.call(instance, tableContainer, d.body);
134
            this.Section.call(instance, tableContainer, d.footer, "tfoot");
135
            if(instance.rq !== null){
136
                var hover = document.getElementById(instance.rq.tableId)
137
                        .getElementsByTagName("th")[instance.rq.colNo].lang;
138
                if(hover){
139
                    instance.ColumnHover(tableContainer, instance.rq.colNo);
140
                }
141
            }
142
        }
143
    },
144
    Export: function(lnk, eType){
145
        var rq = {};
146
        rq.tableId = this.helper.GetParent(lnk, "table").getAttribute("id");
147
        this.BuildRequest.Run(rq);
148
        rq.exportType = ["CSV", "Excel"].indexOf(eType) >= 0 ?
149
                eType :
150
                "csv";
151
        window.open(this.helper.RequestToUrl(rq));
152
        return false;
153
    },
154
    Filter: {
155
        Run: function(field){
156
            var crntTableId = this.Filter.GetTableId(field);
157
            if(crntTableId !== null){
158
                /*var exRq = this.rq;*/
159
                var rq = {};
160
                rq.tableId = crntTableId;
161
                rq.strDesc = this.strDesc;
162
                rq.strAsc = this.strAsc;
163
                this.BuildRequest.Run(rq);
164
                this.rq = rq;
165
                this.LoadData.Run(this);
166
                //@todo exRq match not work
167
                /*if(exRq === null
168
                        || typeof this.rq.filer === "undefined"
169
                        || this.rq.filter !== exRq.filter
170
                        || this.rq.filterBy !== exRq.filterBy
171
                        ){
172
                    this.LoadData.Run(this);
173
                }*/
174
            }
175
        },
176
        GetTableId: function(field){
177
            if(field.tagName.toLowerCase() !== "select"){
178
                return field.getAttribute("data-table-id");
179
            }
180
            var f = field.parentNode.parentNode.getElementsByTagName("input")[0];
181
            return '' === f.value ? null : f.getAttribute("data-table-id");
182
        }
183
    },
184
    GoPage: {
185
        Run: function(lnk){
186
            var rq = {};
187
            rq.tableId = this.helper.GetParent(lnk, "table").getAttribute("id");
188
            rq.strDesc = this.strDesc;
189
            rq.strAsc = this.strAsc;
190
            this.BuildRequest.Run(rq);
191
            rq.pageNo = this.GoPage.GetNo(lnk, rq.tableId);
192
            this.rq = rq;
193
            this.LoadData.Run(this);
194
            return false;
195
        },
196
        GetNo: function(lnk, tableId){
197
            //check & serve pagination jump links
198
            var jumpDir = lnk.innerHTML.trim().substr(0, 1);
199
            if(jumpDir === "+" || jumpDir === "-"){
200
                var current = document.getElementById(tableId)
201
                        .querySelector("tfoot .paging .a").innerHTML;
202
                var jump = lnk.innerHTML.replace("K", "000")
203
                        .replace("M", "000000000");
204
                var jumpPage = (parseInt(current) + parseInt(jump));
205
                lnk.parentNode.setAttribute("data-page", jumpPage);
206
                lnk.style.transform = "none";
207
            }
208
            return lnk.parentNode.hasAttribute("data-page") ?
209
                    lnk.parentNode.getAttribute("data-page") :
210
                    lnk.innerHTML;
211
        }
212
    },
213
    LoadData: {
214
        tail: null,
215
        Run: function(instance){
216
            if(this.tail !== null){
217
                this.tail.abort();
218
            }
219
            this.SetVisability(instance.rq.tableId, false);
220
            var inst = instance;
221
            var xmlhttp = window.XMLHttpRequest ?
222
                    new XMLHttpRequest() : //IE7+, Firefox, Chrome, Opera, Safari
223
                    new window.ActiveXObject("Microsoft.XMLHTTP");//IE6, IE5
224
            xmlhttp.onreadystatechange = function(){
225
                if(xmlhttp.readyState === 4 && xmlhttp.status === 200){
226
                    var d = JSON.parse(xmlhttp.responseText);
227
                    inst.Draw.Run(inst.rq.tableId, d, inst);
228
                    inst.LoadData.SetVisability(inst.rq.tableId, true);
229
                    window.table.LoadEndCalback(inst.rq.tableId);
230
                }
231
            };
232
            xmlhttp.open("GET", instance.helper.RequestToUrl(inst.rq), true);
233
            xmlhttp.send();
234
            this.tail = xmlhttp; //put at tail to can abort later previous request
235
        },
236
        SetVisability: function(tableContainer, flag){
237
            var tbl = document.getElementById(tableContainer);
238
            if(flag === true){
239
                tbl.style.filter = "none";
240
                tbl.style.opacity = "1";
241
                tbl.style.cursor = "auto";
242
            }else if(flag === false){
243
                tbl.style.filter = "blur(1px)";
244
                tbl.style.opacity = "0.8";
245
                tbl.style.cursor = "wait";
246
            }else{
247
                console.error("table error in the flag value");
248
            }
249
        }
250
    },
251
    ReloadData: function(tableId){
252
        var inst = this;
253
        var rq = this.helper.RqObject.call(inst, tableId);
254
        this.BuildRequest.Run(rq);
255
        this.rq = rq;
256
        this.LoadData.Run(this);
257
    },
258
    Sort: function(colNo, lnk){
259
        var rq = {};
260
        rq.tableId = this.helper.GetParent(lnk, "table").getAttribute("id");
261
        rq.strDesc = this.strDesc;
262
        rq.strAsc = this.strAsc;
263
        this.BuildRequest.Run(rq);
264
        if(Math.round(colNo) === rq.colNo){
265
            rq.colOrd = (rq.colOrd === "asc" ? "desc" : "asc");
266
        }else{
267
            rq.colNo = Math.round(colNo);
268
            rq.colOrd = "asc";
269
        }
270
        this.rq = rq;
271
        this.LoadData.Run(this);
272
        ClearAndSetNewSortArrow(rq);
273
274
        function ClearAndSetNewSortArrow(rq){
275
            var headSpans = document.getElementById(rq.tableId)
276
                    .getElementsByTagName("thead")[0]
277
                    .getElementsByTagName("span");
278
            var length = headSpans.length;
279
            for(var i = 0; i < length; i++){
280
                headSpans[i].innerHTML = "";
281
            }
282
            lnk.getElementsByTagName("span")[0].innerHTML =
283
                    (rq.colOrd === "desc" ? rq.strDesc : rq.strAsc);
284
        }
285
    }
286
};
287